package vn.com.epi.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

public class MakeTest {
	
	int nUsers;
	Map<Integer, Integer> categoryMap;
	Map<String, Integer> userID;
	Multimap<Integer, Integer> history;
	int[] cat = {45,47,50,52,53,54,55,58,59,71,72,76,79,82,84,87,88,104,107,108,119,121,122,126,127,132,136,137,138,139,141,144,145,146,147,148,150,152};
	
	int getcategoryId(int category) {
		int first = 0;
		int last = cat.length - 1;
		while (first <= last) {
			int mid = (first + last)/2;
			if (cat[mid] == category)
				return mid + 1;
			else
				if (cat[mid] < category)
					first = mid + 1;					
				else
					last = mid - 1;					
		}
		int index = -1;
		return index;
	}
	
	public void realTest() throws IOException {
		nUsers = 0;
		userID = new HashMap<String, Integer>();
		history = HashMultimap.create();
		categoryMap = new HashMap<Integer, Integer>();
		String[] filenames = new String[] {"input/2014-8-20", "input/2014-8-21"};
		for ( int i = 0; i < filenames.length; i++) {
			File[] files = new File(filenames[i]).listFiles();
			for (File file : files) {
				if (file.isFile()) {
					BufferedReader reader = new BufferedReader(new FileReader(file));
					String line = null;
					while ((line = reader.readLine()) != null) {
						String[] chunks = line.split(",");
						String user = chunks[1];
						int categoryId = Integer.parseInt(chunks[4]);
						categoryId = getcategoryId(categoryId);
						if (categoryId == -1) {
							continue;
						}
						if (!user.startsWith("RANDOM") && user.length() != 36) {							
							int userid;
							int articleID = Integer.parseInt(chunks[3]);
							if (!userID.containsKey(user)) {
								nUsers++;
								userid = nUsers;
								userID.put(user, nUsers);
							} else {
								userid = userID.get(user);
							}
							if (!categoryMap.containsKey(articleID))
								categoryMap.put(articleID, categoryId);
							history.put(userid, articleID);
						}
					}
					reader.close();
					System.out.println(file.getAbsolutePath());
				}
			}
		}
		System.out.println("Number of users: " + nUsers);
		File file = new File("data.csv");		 
		// if file doesnt exists, then create it
		if (!file.exists()) {
			file.createNewFile();
		}
		FileWriter fw = new FileWriter(file.getAbsoluteFile());
		BufferedWriter bw = new BufferedWriter(fw);
		for (int userid: history.keySet()) {
			bw.write(userid + "\t");
			Map<Integer, Integer> map = new HashMap<Integer, Integer>();
			StringBuilder sb = new StringBuilder();
			for (int article: history.get(userid)) {
				if (categoryMap.containsKey(article)) {
					int categoryId = categoryMap.get(article);
					if (map.containsKey(categoryId)) {
						int num = map.get(categoryId);
						num++;
						map.put(categoryId, num);
					} else
						map.put(categoryId, 1);
				}
			}

			for (Map.Entry<Integer, Integer> entry:map.entrySet()) {
//				sb.append("[").append(entry.getKey()).append(",").append(entry.getValue()).append("]").append(" ");
				sb.append(entry.getKey()).append(",").append(entry.getValue()).append(" ");
			}
			if (sb.length() > 0)
				sb.deleteCharAt(sb.length() - 1);
			bw.write(sb.toString() + "\n");
		}
		bw.close();
		
		System.out.println("Xong file data");
		file = new File("history.csv");		 
		// if file doesnt exists, then create it
		if (!file.exists()) {
			file.createNewFile();
		}
		fw = new FileWriter(file.getAbsoluteFile());
		bw = new BufferedWriter(fw);
		for (int userid: history.keySet()) {
			StringBuilder sb = new StringBuilder();
			for (int article: history.get(userid)) {
				sb.append(article).append(",");
			}
			if (sb.length() > 0) {
				sb.deleteCharAt(sb.length() - 1);
				bw.write(userid + "\t" + sb.toString() + "\n");
			}
		}
		bw.close();
		System.out.println("Xong file history");
		file = new File("category.csv");		 
		// if file doesnt exists, then create it
		if (!file.exists()) {
			file.createNewFile();
		}
		fw = new FileWriter(file.getAbsoluteFile());
		bw = new BufferedWriter(fw);
		for (int articleid: categoryMap.keySet()) {
			bw.write(articleid + "," + categoryMap.get(articleid) + "\n");
		}
		bw.close();		
	}
	
		
	public static void main(String[] args) throws IOException {
		MakeTest mk = new MakeTest();
		mk.realTest();
	}

}
